package com.shaoshaohuo.app.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.support.annotation.Nullable;
import android.text.TextPaint;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;

import com.shaoshaohuo.app.R;

/**
 * Created by Administrator on 2017/6/3.
 */
//
public class NumberView extends View {
    private Paint mPaint;

    private String num="0";
    private TextPaint textPaint;

    public NumberView(Context context) {
        this(context, null);
    }

    public NumberView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }
    public NumberView(Context context, @Nullable AttributeSet attrs, int a) {
        super(context, attrs, a);
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.NumView);
        float dimension = typedArray.getDimension(R.styleable.NumView_textsize, 32);
        mPaint = new Paint();
        mPaint.setAntiAlias(true);
        mPaint.setStyle(Paint.Style.FILL);
        mPaint.setColor(Color.parseColor("#ff0000"));
        //字體的paint
        textPaint = new TextPaint();
        textPaint.setAntiAlias(true);
        textPaint.setStyle(Paint.Style.FILL);
        textPaint.setColor(Color.WHITE);
        textPaint.setTextSize(dimension);
        typedArray.recycle();
    }
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int size = MeasureSpec.getSize(widthMeasureSpec)/4;
        MeasureSpec.makeMeasureSpec(0,MeasureSpec.UNSPECIFIED);
        int ws =size+getPaddingLeft()+getPaddingRight();
        setMeasuredDimension(ws, heightMeasureSpec);

    }
    private int getsize(int sp, int hw) {
        int mode = MeasureSpec.getMode(sp);
            Log.e("TAG", "getsize: "+hw );
            if (hw == 0) {
                return ((ViewGroup) getParent()).getMeasuredWidth() / 4;
            } else {
                return ((ViewGroup) getParent()).getHeight() / 4;
            }
    }
    @Override
    protected void onDraw(Canvas canvas) {
        int radios = getWidth() > getHeight() ? getHeight() : getWidth();
        canvas.drawCircle(getWidth() / 2, getHeight() / 2, radios / 2, mPaint);
        if(Integer.valueOf(num)<=99){
            canvas.drawText(num, getWidth() * 0.5f - textPaint.measureText(num) * 0.5f, getHeight() / 2 + textPaint.getFontMetrics().bottom * 1.5f, textPaint);
        }else {
            canvas.drawText("..", getWidth() * 0.5f - textPaint.measureText("..") * 0.5f, getHeight() / 2 + textPaint.getFontMetrics().bottom * 1.5f, textPaint);
        }

    }
    public void setNum(String num) {
        this.num = num;
        invalidate();
    }
    public int getnum(){
        return Integer.valueOf(num);
    }
}
